package cn.easyar.samples.helloarqrcode;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.easyar.samples.helloarqrcode.xunfei.VoiceBean;
import cn.easyar.samples.helloarqrcode.xunfei.VoiceOrder;


/**
 * 异常排查分析
 */
public class AnalysisActivity extends AppCompatActivity {
    Button btn_back;
    TextView tvToolbarCenter;
    RecyclerView rvAnalysisList;

    private Context context;
    private List<String> mList = new ArrayList<String>();
    private List<String> handleDetailsList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去除title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //去掉Activity上面的状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_analysis);
        EventBus.getDefault().register(this);
        VoiceOrder.activityList.add("AnalysisActivity");
        context = this;
        btn_back = findViewById(R.id.btn_back);
        tvToolbarCenter = findViewById(R.id.tv_toolbar_center);
        rvAnalysisList = findViewById(R.id.rv_analysis_list);
        tvToolbarCenter.setText("异常排查分析");

        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initData();

        AnalysisAdapter analysisAdapter = new AnalysisAdapter(mList);
        rvAnalysisList.setLayoutManager(new LinearLayoutManager(context));
        rvAnalysisList.addItemDecoration(new DividerItemDecoration(context,DividerItemDecoration.VERTICAL));
        rvAnalysisList.setAdapter(analysisAdapter);
        analysisAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(AnalysisActivity.this, HandleDetailsActivity.class);
                intent.putExtra("handleDetails", handleDetailsList.get(position));
                intent.putExtra("abnormal", mList.get(position));
                startActivity(intent);
            }
        });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void voiceEvent(VoiceBean voiceBean) {
        if(voiceBean.str.contains(VoiceOrder.VOICE_QUIT)&&VoiceOrder.activityList.get(VoiceOrder.activityList.size()-1).equals("AnalysisActivity")){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    finish();
                }
            });
        }else if(voiceBean.str.contains(VoiceOrder.VOICE_ONE)){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(AnalysisActivity.this, HandleDetailsActivity.class);
                    intent.putExtra("handleDetails", handleDetailsList.get(0));
                    intent.putExtra("abnormal", mList.get(0));
                    startActivity(intent);
                }
            });
        }else if(voiceBean.str.contains(VoiceOrder.VOICE_TWO)){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(AnalysisActivity.this, HandleDetailsActivity.class);
                    intent.putExtra("handleDetails", handleDetailsList.get(1));
                    intent.putExtra("abnormal", mList.get(1));
                    startActivity(intent);
                }
            });
        }else if(voiceBean.str.contains(VoiceOrder.VOICE_THREE)){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(AnalysisActivity.this, HandleDetailsActivity.class);
                    intent.putExtra("handleDetails", handleDetailsList.get(2));
                    intent.putExtra("abnormal", mList.get(2));
                    startActivity(intent);
                }
            });
        }
    }

    private void initData() {
        mList.clear();
        mList.add("冷却水量不足");
        mList.add("冷却器冷却能力下降");
        mList.add("冷却管表面积污垢");
        mList.add("冷却管破裂或管子与管板间的配合松动");
        mList.add("冷却器水侧通道积有气泡");
        mList.add("运行点过分偏离设计点");

        handleDetailsList.clear();
        handleDetailsList.add("检查冷却水流量、压力和温度是否正常，重新调整水压、水温，加大冷却水量");
        handleDetailsList.add("检查冷却水流量，要使冷却器管中的水流速大于2m/s");
        handleDetailsList.add("检查冷却器温差，看冷却器是否由于结垢而使冷却效果下降，清洗冷却器芯子");
        handleDetailsList.add("堵塞已损坏管子的两端或用胀管器将松动的管端胀紧");
        handleDetailsList.add("检查冷却器水侧通道是否有气泡产生，打开放气阀把气体排出");
        handleDetailsList.add("检查实际运行点是否过分偏离规定的操作点，适当调整运行工况");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        VoiceOrder.activityList.remove(VoiceOrder.activityList.size()-1);
        EventBus.getDefault().unregister(this);
    }
}
